package main

import (
	"fmt"
)

func f1() {
	fmt.Println("开始")
	defer func() {
		fmt.Println("aaaa")
	}()
	fmt.Println("结束")
}
func f2() int { //匿名返回值
	var a int

	fmt.Println("结束")
	defer func() {
		a++
	}()
	return a //返回的值为0
}
func f3() (a int) { //命名返回值  return指令不是原子的，先赋值在ret  defer在中间

	fmt.Println("结束")
	defer func() {
		a++
	}()
	return a //返回的值为1
}

// defer 注册要执行的延迟执行的函数时所有的参数都需要确定其值

func calc(index string, a, b int) int {
	ret := a + b
	fmt.Println(index, a, b, ret)
	return ret
}

func main() {
	//代码分析

	/**
	注册顺序：
		defer calc("AA", x, calc("A", x, y))
		defer calc("BB", x, calc("B", x, y))
	执行顺序:
		defer calc("BB", x, calc("B", x, y))
		defer calc("AA", x, calc("A", x, y))
	会打印四个结果
	*/
	x := 1
	y := 2
	defer calc("AA", x, calc("A", x, y))
	x = 10
	defer calc("BB", x, calc("B", x, y))
	y = 20
}
